import pandas as pd
import os
import glob
import re
from program.pack.append_df_to_excel import append_df_to_excel

import numpy as np
import warnings
# 导入shutil模块和os模块
import shutil, os

# 忽略警告
warnings.filterwarnings("ignore")

# 列显示不全，进行设置
pd.set_option('display.max_columns', 500)
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
pd.set_option('display.width', 180)  # 设置打印宽度(**重要**)

# 查询指定execl [data_xlsx]中，所有的第3张图片（正面、背面）图，并按照规则进行重命名图片，存放至path_out文件夹中

# 样品名称
k_name = 'YICM3'
path = r'..\\data\\'+k_name
# 表格需要存在标记坐标
path_execl = r'..\\data\xinjiang_0716_data.xlsx'
isExists = os.path.exists(path)
if not isExists:
    os.makedirs(path)
    print(path+'创建成功')
else:
    print(path+'目录已存在')

data_xlsx = pd.read_excel(path_execl, sheet_name='data')
data_xlsx = data_xlsx.drop(data_xlsx[data_xlsx['样品'] != k_name].index)
data_01 = data_xlsx.drop(data_xlsx[data_xlsx['文件名'] == '2021-07-19-28#-jichang-1.xlsx'].index)
data_01['fb选择'] = data_01['正_o'] - data_01['反_o']


def func_bmi(fb):
    if fb >= 0:
        return 'front_3'
    else:
        return 'back_3'

data_01['fb选择'] = data_01['fb选择'].apply(func_bmi)
data_01['地址'] = data_01['originImage地址'] + '\\' + data_01['originImage文件名'] + '\\' + data_01['fb选择'] + '.bmp'
data_01['fb序号'] = range(55, len(data_01) + 55)
data_01['fb序号'] = data_01['fb序号'].apply(lambda x: str(x).rjust(3, '0'))
data_01['fb序号'] = data_01['fb序号'] + '_' + data_01['部位编号'] + '_' + data_01['样品']
# 6位数，补0
# data_01['fb序号'] = data_01['fb序号'].apply(lambda x: str(x).rjust(6, '0'))

append_df_to_excel(path+'\\'+k_name+'.xlsx', data_01, sheet_name='data', startcol=0, startrow=0, index=False)
# 复制文件
data_01 = data_01.reset_index()

path_out = r'..\\data\\' + k_name + '\\\\'
for i in range(len(data_01)):
    file_path1 = data_01.loc[i, '地址']
    file_path2 = path_out + data_01.loc[i, 'fb序号'] + '.bmp'
    shutil.copy(file_path1, file_path2)
